package com.geek.concurrence4work;

import java.util.Objects;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Author: lishuangqiang
 * @Date: 2021/11/28
 * @Description:
 */

public class LockConditionMethod {

    public static class MyThread extends Thread {
        private volatile Long res = null;
        private Lock lock = new ReentrantLock();
        private Condition condition = lock.newCondition();

        private MyThread() {
        }


        @Override
        public void run() {
            lock.lock();
            try {
                res = Homework03.sum3();
                condition.signal();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }

        public long getRes() throws InterruptedException {
            try {
                lock.lock();
                if (Objects.isNull(res)) {
                    condition.await();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
            return res;
        }
    }

    public static void main(String[] args) throws InterruptedException {
        MyThread myThread = new MyThread();
        myThread.start();
        long res = myThread.getRes();
        System.out.println(res);
    }
}
